const skeletonData = require('./skeletonData');
const {
  _navigateTo
} = require("../../utils/util.js");
//index.js
//获取应用实例
const {
  getSwiper,
  getFeature,
  getCourseList,
  getCategoryList
} = require("../../api/api.js");
const {
  fly
} = require("../../api/request.js");
const app = getApp();
Page({
  data: {
    pageIsReady: false,
    skeletonLoadingTypes: ['spin', 'chiaroscuro', 'shine', 'null'],
    skeletonSelectedLoadingType: 'shine',
    skeletonIsDev: false,
    skeletonBgcolor: '#FFF',
    skeletonData,
    swiperData: [],
    Featrue: [],
    courseList: [],
    courseCategory: [],
    imgHost: ''
  },
  onLoad: function () {
    /**
     * 获取轮播数据
     */
    app.getHost().then(({
      host,
      img_host
    }) => {
      fly.config.baseURL = host;
      this.setData({
        imgHost: img_host
      })
      wx.getSetting({
        success: res => {
          if (!res.authSetting['scope.userInfo']) {
            wx.reLaunch({
              url: '/pages/login/index'
            })
          }
        }
      })
      const promiseQueue = [getSwiper(), getFeature(), getCourseList(), getCategoryList()];
      Promise.all(promiseQueue).then((values) => {
        this.setData({
          swiperData: values[0].data,
          Featrue: values[1].data,
          courseList: values[2].data,
          courseCategory: values[3].data,
          pageIsReady: true
        })
      });

    })
  },
  changeTabHandler(e) {
    let type = e.detail === 0 ? '' : e.detail;
    getCourseList({
      type: type
    }).then(res => {
      this.setData({
        courseList: res.data
      })
    }).catch(err => {
      console.log(err)
    })
  },
  /**
   * 监听父组件事件到 公司简介页面
   */
  navigatetocompany() {
    _navigateTo("../index/company-intro/index")
  },
  /**
   * 监听父组件事件到 联系我们页面
   */
  navigatetoaboutus() {
    _navigateTo("../index/about-us/index")
  },
  /**
   * 监听父组件事件到 到资料库列表页
   */
  navigatetodatabank() {
    _navigateTo("./data-bank/index")
  },
  /**
   * 监听父组件事件到 到分类页面
   *  {wx.reLaunch} 跳转到 tabBar 页面，并关闭其他所有非 tabBar 页面
   */
  navgatetocategory() {
    wx.switchTab({
      url: '../category/category',
    });
  },
  /**
   * 跳转到专栏指定详情页
   */
  changetofeaturedetail(e) {
    const id = e.currentTarget.dataset;
    console.log("index页面的专栏列表")
    _navigateTo(`./feature-list/feature-detail/index?id=${id}`);
  },
  /**
   * 点击跳转到指定详情页面
   */
  toanydetail(e) {
    const {
      id,
      type
    } = e.detail;
    console.log(id, type)
    if (type === 1) {
      _navigateTo(`./course-list/video-detail/index?id=${id}`);
    } else if (type === 2) {
      _navigateTo(`./course-list/audio-detail/index?id=${id}`);
    } else if (type === 3) {
      _navigateTo(`./course-list/text-detail/index?id=${id}`);
    }
  }
})